package com.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.netca.bmps.userservice.bo.CertInfo;
import net.netca.bmps.userservice.bo.CertInfoLocal;
import net.netca.bmps.userservice.service.UserService;
import net.netca.bmps.userservice.servicelayer.UserServiceImpl;
import net.netca.gdltax.bpms.businessmanage.utilits.Base64Encoder;
import net.netca.gdltax.bpms.businessmanage.utilits.Converter;
import net.netca.gdltax.bpms.webservice.CertServiceClient;
import net.netca.gdltax.bpms.webservice.CertServicePortType;
import net.netca.testwebservice.bo.XMLConfig;
import net.netca.util.encoders.Base64;

@SuppressWarnings("serial")
public class ReqAndInstall extends HttpServlet {
	public ReqAndInstall() {
		super();
	}

	public void destroy() {
		super.destroy();
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		
		try {
			CertServiceClient client = new CertServiceClient(XMLConfig.getInstance().getUrl());
			CertServicePortType service = client.getCertServiceHttpPort();

			// 申请信息
			String name = request.getParameter("name");
			String taxpayerid = request.getParameter("taxpayerid");
			String subuserid = request.getParameter("subuserid");
			int validity = Integer.parseInt(request.getParameter("validity"));
			int caid = Integer.parseInt(request.getParameter("caid"));
			int keytype = Integer.parseInt(request.getParameter("keytype"));
			String keysn = request.getParameter("keysn");
			int iswebuser = Integer.parseInt(request.getParameter("iswebuser"));

			String p10b64 = request.getParameter("p10");
			byte[] p10 = Base64Encoder.decode(p10b64);
			String soPin = request.getParameter("soPin");
			String userPin = request.getParameter("userPin");

			// 获取返回的证书
			CertInfoLocal certInfo = new CertInfoLocal();
			CertInfo info = null;
			info = service.requstInstallCert(name, taxpayerid, subuserid,
					validity, caid, keytype, keysn, p10, iswebuser);
			service.saveSoPin(keysn, soPin);

			// 获取受理号
			String reqno = null;// =info.getReqno();
			String pemsigncert = null;
			String pemenccert = null;
			String encpair = null;
			Converter.CertInfoConvert(certInfo, info);
			UserService userService = new UserServiceImpl();

			if (certInfo.getSigncert() != null) {
				certInfo.setX509SignCert(userService.getNetCACert(certInfo.getSigncert()));
			}
			if (certInfo.getEnccert() != null) {
				certInfo.setX509EncCert(userService.getNetCACert(certInfo.getEnccert()));
			}
			if (certInfo != null) {
				if (certInfo.getX509SignCert() != null || certInfo.getX509EncCert() != null) {
					byte[] bsigncert = certInfo.getX509SignCert().getEncodeValue();
					byte[] benccert = certInfo.getX509EncCert().getEncodeValue();
					byte[] bencpair = certInfo.getEnckeypair();
					reqno = certInfo.getReqno();
					pemsigncert = Base64.encodeToString(bsigncert, 0,
							bsigncert.length, Base64.BASE64_FLAG_NOCRLF);
					pemenccert = Base64.encodeToString(benccert, 0,
							benccert.length, Base64.BASE64_FLAG_NOCRLF);

					if (bencpair == null || bencpair.length == 0) {
						encpair = "";
					} else {
						encpair = Base64.encodeToString(bencpair, 0,
								bencpair.length, Base64.BASE64_FLAG_NOCRLF);
					}
				}
			}// End if

			// 安装时需要的数据
			request.getSession().setAttribute("pemsigncert", pemsigncert);
			request.getSession().setAttribute("pemenccert", pemenccert);
			request.getSession().setAttribute("encpair", encpair);
			request.getSession().setAttribute("certId", certInfo.getId());
			request.getSession().setAttribute("reqno", reqno);
			request.getSession().setAttribute("userPin", userPin);

			response.setCharacterEncoding("UTF-8");
			response.sendRedirect("../install.jsp");

		} catch (Exception e) {
			e.printStackTrace();
			request.getSession().setAttribute("exception", e);
			response.sendRedirect("../showerror.jsp");
		}
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doGet(request, response);
	}

	public void init() throws ServletException {
	}
}
